package com.fastjrun.eladmin.itrms.service.impl;

import com.fastjrun.eladmin.itrms.dao.RmsDeploymentDao;
import com.fastjrun.eladmin.itrms.dto.DeploymentDTO;
import com.fastjrun.eladmin.itrms.dto.DeploymentQueryDTO;
import com.fastjrun.eladmin.itrms.entity.RmsDeployment;
import com.fastjrun.eladmin.itrms.entity.RmsDeploymentExample;
import com.fastjrun.eladmin.itrms.mapstruct.RmsDeploymentMapper;
import com.fastjrun.eladmin.itrms.service.RmsDeploymentControllerService;
import com.fastjrun.eladmin.itrms.utils.PageUtils;
import com.fastjrun.eladmin.itrms.vo.DeploymentVO;
import com.fastjrun.eladmin.vo.PageResult;
import com.github.pagehelper.PageInfo;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service("rmsDeploymentControllerService")
public class RmsDeploymentControllerServiceImpl extends BaseItrmsService implements RmsDeploymentControllerService {

    @Resource
    RmsDeploymentDao rmsDeploymentDao;
    @Resource
    RmsDeploymentMapper rmsDeploymentMapper;

    private RmsDeploymentExample retrive(DeploymentQueryDTO queryDTO) {
        log.debug("queryDTO={}", queryDTO);
        RmsDeploymentExample rmsDeploymentExample = new RmsDeploymentExample();
        RmsDeploymentExample.Criteria criteria = rmsDeploymentExample.createCriteria();
        if (queryDTO != null) {
            if (queryDTO.getDeployType() != null && queryDTO.getDeployType() != 0) {
                criteria.andDeployTypeEqualTo(queryDTO.getDeployType());
            }
            if (queryDTO.getDeployWay() != null && queryDTO.getDeployWay() != 0) {
                criteria.andDeployWayEqualTo(queryDTO.getDeployWay());
            }
        }
        return rmsDeploymentExample;
    }

    @Override
    public PageResult<DeploymentVO> query(DeploymentQueryDTO queryDTO, Pageable pageable) {

        if (pageable == null) {
            pageable = PageRequest.of(PAGE_CURRENT, PAGE_SIZE);
        }

        PageUtils.startPage(pageable);
        RmsDeploymentExample rmsDeploymentExample = this.retrive(queryDTO);
        String orderClause=this.getOrderClause(pageable);
        if(StringUtils.isNotBlank(orderClause)){
            rmsDeploymentExample.setOrderByClause(orderClause);
        }

        PageInfo<RmsDeployment> pageInfo = new PageInfo<>(rmsDeploymentDao.selectByExampleWithBLOBs(rmsDeploymentExample));
        List<DeploymentVO> list = rmsDeploymentMapper.toVO(pageInfo.getList());

        PageResult<DeploymentVO> pageResult = new PageResult<>();
        pageResult.setContent(list);
        pageResult.setTotalElements((int) pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public void create(DeploymentDTO createDTO) {
        RmsDeployment rmsDeployment = this.rmsDeploymentMapper.toEntity(createDTO);
        rmsDeployment.setCreateUser(SecurityUtils.getCurrentUsername());
        rmsDeployment.setCreateUserId(SecurityUtils.getCurrentUserId());
        rmsDeployment.setOwnerUser(sysUserDao.selectByPrimaryKey(createDTO.getOwnerUserId()).getUsername());
        rmsDeployment.setCreateDate(new Date());
        rmsDeployment.setUpdateDate(new Date());
        this.rmsDeploymentDao.insert(rmsDeployment);

    }

    @Override
    public void update(DeploymentDTO updateDTO) {
        RmsDeployment rmsDeployment = this.rmsDeploymentMapper.toEntity(updateDTO);
        rmsDeployment.setOwnerUser(sysUserDao.selectByPrimaryKey(updateDTO.getOwnerUserId()).getUsername());
        rmsDeployment.setUpdateDate(new Date());
        this.rmsDeploymentDao.updateByPrimaryKeySelective(rmsDeployment);

    }

    @Override
    public void delete(Long[] ids) {
        RmsDeploymentExample rmsDeploymentExample = new RmsDeploymentExample();
        rmsDeploymentExample.createCriteria().andIdIn(Arrays.asList(ids));
        this.rmsDeploymentDao.deleteByExample(rmsDeploymentExample);
    }
}
